﻿using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UMLSynthesizer.CSharpAnalyzer.Helpers;
using UMLSynthesizer.LangAnalyzer.Expressions;
using UMLSynthesizer.LangAnalyzer.Statements;

namespace UMLSynthesizer.CSharpAnalyzer.Statements
{
    class CSharpForStatement : CSharpStatementBase, IForStatement
    {
        #region Fields

        CSharpStatementBlockHelper blockHelper;
        CSharpLocalDeclarationStatement _declaration;
        CSharpExpressionHelper controlExpressionHelper;
        CSharpExpressionHelper incrementorExpressionHelpers;

        #endregion

        #region Constructor

        public CSharpForStatement(ForStatementSyntax statementNode, IStatementBase parentStatement)
            : base(statementNode, parentStatement, null, StatementKinds.For)
        {
            blockHelper = new CSharpStatementBlockHelper(this,
                () =>
                {
                    if (statementNode.Statement.CSharpKind() == SyntaxKind.Block)
                        return (statementNode.Statement as BlockSyntax).Statements;
                    else
                        return new StatementSyntax[] { statementNode.Statement };
                });
            controlExpressionHelper = new CSharpExpressionHelper(null, this,
                () => statementNode.Condition);
            incrementorExpressionHelpers = new CSharpExpressionHelper(null, this,
                () => statementNode.Incrementors);
        }

        #endregion

        #region Properties

        public ILocalDeclarationStatement Declaration
        {
            get
            {
                if (_declaration == null)
                    _declaration = new CSharpLocalDeclarationStatement((statementNode as ForStatementSyntax).Declaration, this);
                return _declaration;
            }
        }
        public IExpressionBase ControlExpression { get { return controlExpressionHelper.Expression; } }
        public IEnumerable<IExpressionBase> IncrementorExpressions { get { return incrementorExpressionHelpers.Expressions; } }
        public IEnumerable<IStatementBase> ContainedStatements { get { return blockHelper.ContainedStatements; } }

        #endregion
    }
}
